package com.epoch.servicecourse.service.impl;

import com.alibaba.druid.util.StringUtils;

import com.epoch.servicecourse.bean.Video;
import com.epoch.servicecourse.dao.VideoDao;
import com.epoch.servicecourse.service.VideoService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.epoch.servicecourse.util.NonStaticResourceHttpRequestHandler;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 *
 * @author Seven
 * @date 2021/11/14
 */
@Service
@Slf4j
@AllArgsConstructor
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoDao videoDao;

    private final NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;

    @Override
    public void getVideo(Integer videoId, HttpServletResponse response, HttpServletRequest request){

        Video video = videoDao.getVideoByVideoId(videoId);
        System.out.println("videoId: "+videoId);
        System.out.println("video: "+video);
        String sourcePath = "F:\\epoch download\\";
        String realPath = sourcePath + "temp.mp4";

        //创建一个新的 文件 保存从数据库读出的视频
        File tempFile = new File(realPath);
        if (!tempFile.exists()){
            try {
                tempFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //讲视频写入文件
        //获取视频内容
        try{
            //创建文件字节输出流
            FileOutputStream fos = new FileOutputStream(realPath);
            byte []data = video.getVideoContent();
            //开始写入数据
            fos.write(data);
            fos.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException i){
            i.printStackTrace();
        }

        //讲数据返回给前端
        Path filePath = Paths.get(realPath);
        if (Files.exists(filePath)){
            try {
                //设置视频格式
                String mimeType = Files.probeContentType(filePath);
                if (!StringUtils.isEmpty(mimeType)){
                    response.setContentType(mimeType);
                }
                request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE,filePath);
                nonStaticResourceHttpRequestHandler.handleRequest(request,response);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        }
    }

}
